package org.saxion.aktiehuis.view;

import java.util.HashMap;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.saxion.aktiehuis.model.Product;


class ProductenTabelModel extends AbstractTableModel{
   
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Product> producten;
	private HashMap<Product ,Integer> productenHash;
	private int editColum;
	private String headerList[] = new String[] {"Product",
            "Soort",
            "Prijs",
            "Aantal"};
    
    public ProductenTabelModel(List<Product> deProducten){

    	producten = deProducten;
    	productenHash = new HashMap<Product ,Integer>();
		
		for(Product product : deProducten)
		{
			productenHash.put(product, 0);
		}
    }

	public Object getValueAt(int rowIndex, int columnIndex) {
		
		switch (columnIndex) {
            case 0:
                return producten.get(rowIndex).getNaam();  
            case 1:
                return producten.get(rowIndex).getProductSoort().getNaam();
            case 2:
                return producten.get(rowIndex).getPrijs();  
            case 3:
            	return productenHash.get(producten.get(rowIndex));
        }
		
		return null; //temp?
	}
	
	public boolean isCellEditable( int row, int col ) {
		return col == editColum;
		
	}
	
	public void setEditableCol(int col)
	{
		editColum = col;
	}

    //display the name of columns
    public String getColumnName(int col) {
    	return headerList[col];     
    }


	public int getColumnCount() {
		return 4;
	}
	
	public int getRowCount() {		
		return producten.size();
	}
	
	 public void setValueAt(Object value, int rowIndex, int columnIndex) {
		 	if(columnIndex == 3)
		 	{
		 		productenHash.put(producten.get(rowIndex), (Integer) value);	
		 	}
        
	 }
	 
	public HashMap<Product ,Integer> getProducts()
	{
		return productenHash;	
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Class getColumnClass( int columnIndex )          
	{ 
		
		switch (columnIndex) {
        case 0:
            return String.class;  
        case 1:
            return String.class;
        case 2:
            return double.class;
        case 3:
            return int.class;
        default:
        	return Object.class;
		}
		
	}
	

}